# @scriptmeta-start
# @name: ${script-name}
# @summary: Checks if there are birthdays coming up in the next see @param(daysAhead)
# @scripttype: device_script
# @company: CodeHelix Solutions Inc.
# @author: kishore reddy | mahesh g.
# @doc:	http://www.codehelixsolutions.com/fluentscriptplugins#upcomingbdays
# @arg: name: daysAhead,   alias: days,     type: number,  default: n/a,     required: yes, examples: 8 | 5
# @arg: name: displayMode, alias: display,  type: text,    default: 'table', required: no,  examples: 'table' | 'list'
# @license: http://www.codehelixsolutions.com
# @script-version: 1.0
# @fluentscript-version: ${fs-version}
# @tags: birthdays, alert, friends
# @isInstalled: true
# @scriptmeta-end

@if debug
	
	set daysAhead = 4
	
@endif


# Note: this represents the final result of the script returned to the fluentscript app
# Note: this fluentscript app will check if the success is true and message if any error
set result = { success: no , data: nothing , format: 'tableview', message: nothing }



# @summary: Checks the contacts to see if there are any upcoming birthdays
# @arg: name: contacts,   type: list(contacts),  required: yes
step check_contacts ( contacts )
{
	# Note: listof( name, birthday, message ) creates a table structure with 3 columns ( name, birthday, message )
	# Note: you can then do table.add column1Value, column2Value, column3Value  
	# Note: for the time being, you can only create tables with 4 columns ( restriction to be removed later )
	set birthdays = listof( name , birthday , message )
	
	# Note: with each plugin makes looping and data access easier:
	# Note: 1. you don't have to write the collection name 'contacts' as in 'for( contact in contacts )
	# Note: 2. inside the with each block, you don't have to prefix 'contact.' as in 'contact.name' you can write 'name'
	with each contact
	{ 
		if birthday is today then			
			birthdays.add ( first, birthday, first + ' birthday today !' )			
        
		else if birthday < 2 days away then 
			birthdays.add ( first, birthday, ' 2 days away' )
		
		else if birthday < daysAhead days away then
			birthdays.add ( first, birthday, ' coming up in ' + daysAhead )
		
	}
	result.data = birthdays
	result.success = any birthdays ?
}


# Note: This setups the asynchronous calls for windows phone but is also compatible for
#       if we ever move to android.

run step check_contacts on get_all_contacts_in_group 'close friends'

@if debug
	
	printresults()
	
@endif